package com.starblink.library.widget.sort

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.animation.Animation
import android.view.animation.DecelerateInterpolator
import android.view.animation.TranslateAnimation
import com.starblink.android.basic.brv.utils.addModels
import com.starblink.android.basic.brv.utils.linear
import com.starblink.android.basic.brv.utils.setup
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.ext.enableCorner
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.util.FontUtil
import com.starblink.library.widget.databinding.LayoutPopPriceSortBinding
import com.starblink.library.widget.databinding.LayoutPopPriceSortItemBinding
import razerdp.basepopup.BasePopupWindow

class PriceSortPop(
    context: Context,
    val selectedSortType: SortTypeEnum? = null, //默认选中的排序类型
    val onSelect: ((priceSortBean: PriceSortBean) -> Unit)? = null, //选中回调
) :
    BasePopupWindow(context) {

    private val binding =
        LayoutPopPriceSortBinding.inflate(LayoutInflater.from(context))

    private val priceSortBeans = mutableListOf(
        PriceSortBean(
            sortType = SortTypeEnum.DEFAULT,
            text = "Recommended",
        ),
        PriceSortBean(
            sortType = SortTypeEnum.PRICE_H2L,
            text = "Price: high to low",
        ),
        PriceSortBean(
            sortType = SortTypeEnum.PRICE_L2H,
            text = "Price: low to high",
        )
    )

    init {
        contentView = binding.root
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        setAlignBackground(true)
        setBackgroundColor(Color.parseColor("#AD0F172A"))
        // isOutSideTouchable = true

        // 设置弹窗内容
        binding.rv.enableCorner(24.dp2px(), "bottom")
        binding.rv.linear().setup {
            addType<PriceSortBean, LayoutPopPriceSortItemBinding>(it)

            onBind {
                val viewBinding = getBinding<LayoutPopPriceSortItemBinding>()
                val model = getModel<PriceSortBean>()

                viewBinding.tvText.text = model.text
                if (model.selected) {
                    viewBinding.ivCheckbox.isSelected = true
                    viewBinding.tvText.setTextColor(color(com.starblink.basic.style.R.color.gu_text_primary))
                    FontUtil.setMontseratSemiBold(viewBinding.tvText)
                } else {
                    viewBinding.ivCheckbox.isSelected = false
                    viewBinding.tvText.setTextColor(color(com.starblink.basic.style.R.color.gu_text_secondary))
                    FontUtil.setMontseratMedium(viewBinding.tvText)
                }
            }

            onClick(com.starblink.library.widget.R.id.root) {
                // 点击列表项，返回选中的排序类型
                val model = getModel<PriceSortBean>()
                onSelect?.invoke(model)
                dismiss()
            }
        }
        // 设置弹窗数据
        priceSortBeans.forEach { it.selected = it.sortType == selectedSortType }
        binding.rv.addModels(priceSortBeans)
    }

    override fun onCreateShowAnimation(): Animation = TranslateAnimation(
        Animation.RELATIVE_TO_PARENT,
        0f,
        Animation.RELATIVE_TO_PARENT,
        0f,
        Animation.RELATIVE_TO_PARENT,
        -1f,
        Animation.RELATIVE_TO_PARENT,
        0f
    ).apply {
        interpolator = DecelerateInterpolator()
        duration = 300
    }

    override fun onCreateDismissAnimation(): Animation = TranslateAnimation(
        Animation.RELATIVE_TO_PARENT,
        0f,
        Animation.RELATIVE_TO_PARENT,
        0f,
        Animation.RELATIVE_TO_PARENT,
        0f,
        Animation.RELATIVE_TO_PARENT,
        -1f
    ).apply {
        interpolator = DecelerateInterpolator()
        duration = 300
    }

    override fun onOutSideTouch(
        event: MotionEvent,
        touchInBackground: Boolean,
        isPressed: Boolean,
    ): Boolean {
        return if (isAllowDismissWhenTouchOutside && isPressed) {
            this.dismiss()
            false
        } else {
            false
        }
    }
}